<%@page contentType="text/html" %>
<%@page pageEncoding="UTF-8" %>
<%@ page import="java.util.Map" %>
<%@ page import="java.util.Iterator" %>
<%@ page import="java.util.List" %>
<%@ page import="org.jasig.cas.client.authentication.AttributePrincipal" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>CAS Example Java Web App</title>
    <style type="text/css">
        table tr td{border: 1px solid #e7e7e7; height: 25px;}
        table tr:nth-child(2n){background-color: #FFFF77}

    </style>
    <script type="text/javascript">
        function confirmAct() {
            if (confirm("确定退出所有系统？")){
                return true;
            }
            return false;
        }

    </script>
</head>
<body>
<% int serverPort = request.getServerPort();
    out.println("<b>端口号:</b>");
    out.println(serverPort);
    out.println("<br>");
    String requestURI = request.getServerName();
    out.println("<b>域名:</b>");
    out.println(requestURI);
%>
<h1 align="center">CAS 当前为客户端(App2)</h1>
<h4><a href="http://app1.cas.com:8081/">APP1</a></h4>
<h4><a href="http://app2.cas.com:8082/">APP2</a></h4>
<hr>
<a onclick="return confirmAct();" href="<%=request.getContextPath() %>/logout2.do" title="Click here to log out">退出系统</a>

<hr>

<p><b>经过身份验证的CAS用户Id:</b> <a href="logout.jsp" title="Click here to log out"><%= request.getRemoteUser() %>
</a></p>
<%
    if (request.getUserPrincipal() != null) {
        AttributePrincipal principal = (AttributePrincipal) request.getUserPrincipal();

        final Map attributes = principal.getAttributes();

        if (attributes != null) {
            Iterator attributeNames = attributes.keySet().iterator();
            out.println("<b>Attributes:</b>");

            if (attributeNames.hasNext()) {
                out.println("<hr><table style=\"border-collapse: collapse\" border='3pt' width='100%'>");
                out.println("<th colspan='2'>登陆用户属性</th>");
                out.println("<tr><td><b>Key</b></td><td><b>Value</b></td></tr>");

                for (; attributeNames.hasNext(); ) {
                    String attributeName = (String) attributeNames.next();
                    if (attributeName.equals("credentialType")||attributeName.equals("isFromNewLogin")||attributeName.equals("authenticationMethod")||attributeName.equals("successfulAuthenticationHandlers")||attributeName.equals("longTermAuthenticationRequestTokenUsed")) {
                        continue;
                    }else {
                        out.println("<tr><td>");
                        out.println(attributeName);
                        out.println("</td><td>");
                        final Object attributeValue = attributes.get(attributeName);

                        if (attributeValue instanceof List) {
                            final List values = (List) attributeValue;
                            out.println("<strong>Multi-valued attribute: " + values.size() + "</strong>");
                            out.println("<ul>");
                            for (Object value : values) {
                                out.println("<li>" + value + "</li>");
                            }
                            out.println("</ul>");
                        } else {
                            out.println(attributeValue);
                        }
                        out.println("</td></tr>");
                    }

                }
                out.println("</table>");
            } else {
                out.print("No attributes are supplied by the CAS server.</p>");
            }
        } else {
            out.println("<pre>The attribute map is empty. Review your CAS filter configurations.</pre>");
        }
    } else {
        out.println("<pre>The user principal is empty from the request object. Review the wrapper filter configuration.</pre>");
    }
%>

</body>
</html>